package org.androidpn.server.xmpp.packet;

import java.util.ArrayList;
import java.util.List;

import org.androidpn.server.entity.RoomMessage;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;

/**
 * packet 游戏房间消息
 * 
 * @author 蔡琪
 * 
 */
public class RoomMessagePacket extends IQ {

	/**
	 * 操作枚举
	 * 
	 * @author 蔡琪
	 * 
	 */
	public enum Operation {
		/** 发送消息 */
		sendMessage,
		/** 接收消息 */
		getMessage,
		/** 推送消息 */
		pushMessage
	}

	/** 游戏房间消息列表 */
	private List<RoomMessage> roomMessages = new ArrayList<RoomMessage>();
	/** 游戏房间消息 */
	private RoomMessage roomMessage;
	/** 操作信息 */
	private Operation operation;

	public String getChildElementXML() {
		StringBuffer xml = new StringBuffer();
		xml.append("<query xmlns=\"jabber:iq:roomMessage\">");
		xml.append("<roomMessages>");
		for (RoomMessage roomMessage : roomMessages) {
			xml.append(createRoomMessageNode(roomMessage));
		}
		xml.append("</roomMessages>");
		if (roomMessage != null) {
			xml.append(createRoomMessageNode(roomMessage));
		}
		if (operation != null && !operation.equals("")) {
			xml.append("<operation>");
			xml.append(operation);
			xml.append("</operation>");
		}
		xml.append("</query>");
		return xml.toString().replaceAll("\\>\\s*|\t|\r|\n", ">");
	}

	/**
	 * 生成roomMessage节点
	 * 
	 * @param roomMessage
	 * @return
	 */
	private String createRoomMessageNode(RoomMessage roomMessage) {
		return "<roomMessage>" + StringUtils.escapeNode(roomMessage.toXML())
				+ ("</roomMessage>");
	}

	public List<RoomMessage> getRoomMessages() {
		return roomMessages;
	}

	public void setRoomMessages(List<RoomMessage> roomMessages) {
		this.roomMessages = roomMessages;
	}

	public RoomMessage getRoomMessage() {
		return roomMessage;
	}

	public void setRoomMessage(RoomMessage roomMessage) {
		this.roomMessage = roomMessage;
	}

	public Operation getOperation() {
		return operation;
	}

	public void setOperation(Operation operation) {
		this.operation = operation;
	}

}
